package com.seeTools.boot.pc.util;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.seeTools.boot.common.constant.SysConstant;
import com.seeTools.boot.common.core.PageQuery;
import org.springframework.stereotype.Component;

@Component
public class PageQueryHelper<T> {

    /**
     * 分页查询
     *
     * @param service
     * @param pageQuery
     * @param queryWrapper
     * @return
     */
    public Page<T> pageInfo(IService<T> service, PageQuery<T> pageQuery, QueryWrapper<T> queryWrapper) {
        if (CharSequenceUtil.isNotEmpty(pageQuery.getSortName())) {
            if (SysConstant.DEFAULT_SORT_ORDER.equalsIgnoreCase(pageQuery.getSortOrder())) {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(pageQuery.getSortName()));
            } else {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(pageQuery.getSortName()));
            }
        }
        return service.page(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), queryWrapper);
    }
}
